/* -----------------------------------------------------------------------
 * Demonstration of how to write unit tests for dominion-base
 * Include the following lines in your makefile:
 *
 * unittest2: unittest2.c dominion.o rngs.o
 *      gcc -o testUpdateCoins -g  unittest2.c dominion.o rngs.o $(CFLAGS)
 * -----------------------------------------------------------------------
 */

#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <time.h>
#include "rngs.h"
int main() {
	//testing villageCard
	int x;
	struct gameState G;
	struct gameState copyG;
	int numPlayer = 2;
	int seed = 2;
	int k[10] = {adventurer, council_room, feast, gardens, mine
		, remodel, smithy, village, baron, great_hall};
	int playCardSuccess = 0;
	
	
	
	//	void villageCard(struct gameState *state, int currentPlayer, int handPos){
	//		//+1 Card
	//		drawCard(currentPlayer, state);
	//
	//		//+2 Actions
	//		state->numActions = state->numActions + 2;
	//
	//		//discard played card from hand
	//		discardCard(handPos, currentPlayer, state, 0);
	//	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////
	
	printf("Testing Village Card\n");
	initializeGame(numPlayer, k, seed, &G); // initialize a new game,
	
	srand(time(NULL));//seed the rand function to time
	
	//want to randomize:
	//  position of card in hand
	//  number of cards in hand
	//  number of actions available
	//  number of cards in draw pile
	//  number of cards in discard pile
	//  different players as well
	
	//initialize to standardish value, then change for random fxn
	int handCount=5;
	int handPos=0;
	int actionCount=1;
	int drawPileCount=5;
	int discardCount=5;
	int player = 0;
	
	
	
	
	
	for (x = 0; x<5000000; x++) {
		//randomize above changes
		
		//the MAX_HAND count is 500. not likely. So only one in 50 times will we test that high.
		//the rest will be reasonbly up to 10
		if (x%50 ==0) {
			handCount = (rand() % MAX_HAND)+1;//we want anywhere from 1 to MAX_HAND
		}
		else{
			handCount = (rand() % 10)+1;//we want anywhere from 1 to 5
		}
		
		
		
		handPos =   (rand() % handCount); // anywhere from 0 to the handCount
		actionCount = (rand() % 5); //want to go from zero up to 5. I think that's enough
		drawPileCount = (rand() % MAX_DECK); //from zero to max deck
		discardCount = (rand() % (MAX_DECK-drawPileCount)); //from zero to what would make up max deck with the draw pile count
		player = (rand() % 1); //player 1 or two
		
		//apply the above changes
		G.handCount[player]= handCount;
		//handPos will be used to add the card to the hand
		G.numActions = actionCount;
		G.deckCount[player] = drawPileCount;
		G.discardCount[player] = discardCount;
		
		//put the village card into the hand at the handPos position
		G.hand[player][handPos]= village;
		
		
		memcpy(&copyG,&G, sizeof(G));//copy everything to a separate hand for comparisons
		
		//play the card
		playCardSuccess = playCard(handPos,0,0,0,&G);//the zeros are choices for different cards than village.
		
		
		//if there are no actions, playCard returns - 1. make sure that works
		if ( (actionCount ==0) && (playCardSuccess== -1) ) {
			//printf("Passed basic card test (no actions)\n");
			continue;
			
		}
		
		
		//after effect, player should have one removed from draw pile, have one added to hand (and taken away, so the same amount), and have
		//+1 actions available
		
		
		//if there is nothing in the deck, then we reshuffled from discard. Check that.
		//if there is also nothing in the discard, then no draw. check that too
		if (drawPileCount ==0) {
			if (discardCount==0) {//no cards to reshuffle
				//in this case, we lose the card we played, and do not replace it. However,
				//we gain an action
				if (
						(G.handCount[player]==(copyG.handCount[player]-1) ) &&
						(G.numActions==(copyG.numActions+1))
						)
				{
					//printf("Passed basic card test\n");
				}
				
				else{
					printf("Failed basic card test\n");
					printf("hand count = %d\n", handCount);
					printf("expected hand count = %d\n", (copyG.handCount[player]-1));
					printf("hand position = %d\n", handPos);
					printf("action count = %d\n", actionCount);
					printf("discard count = %d\n", discardCount);
					printf("draw count = %d\n", drawPileCount);
					printf("player number = %d\n", player);
				}
				
			}
			else{//there were some cards to reshuffle with.
				if ( (G.deckCount[player]== copyG.discardCount[player] -1)  &&
						(G.handCount[player]==(copyG.handCount[player])) &&
						(G.numActions==(copyG.numActions+1)) //playCard is where numActions is decremented, so this will return the +2 without decrementing the card used
						)  {
					//printf("Passed basic card test\n");
				}
				
				else{
					printf("Failed basic card test\n");
					printf("hand count = %d\n", handCount);
					printf("expected hand count = %d\n", (copyG.handCount[player]-1));
					printf("hand position = %d\n", handPos);
					printf("action count = %d\n", actionCount);
					printf("discard count = %d\n", discardCount);
					printf("draw count = %d\n", drawPileCount);
					printf("player number = %d\n", player);
				}
				///////////////////////////////////////////////////////////////////////////////////////////////////////
				
				
				
				
			}
			//we tested everything already. start over
			continue;
			
		}
		
		
		
		
		//not a special case. test as normal
		if ( (G.deckCount[player]==(copyG.deckCount[player] - 1) ) &&
				(G.handCount[player]==(copyG.handCount[player])) &&
				(G.numActions==(copyG.numActions+1)) //playCard is where numActions is decremented, so this will return the +2 without decrementing the card used
				)  {
			//printf("Passed basic card test\n");
		}
		
		else{
			printf("Failed basic card test\n");
			printf("hand count = %d\n", handCount);
			printf("expected hand count = %d\n", (copyG.handCount[player]-1));
			printf("hand position = %d\n", handPos);
			printf("action count = %d\n", actionCount);
			printf("discard count = %d\n", discardCount);
			printf("draw count = %d\n", drawPileCount);
			printf("player number = %d\n", player);
		}
		///////////////////////////////////////////////////////////////////////////////////////////////////////
	}
	
	
	
	printf("If there is nothing above this line, then all tests have passed. \n");
	
	
	
	
	return 0;
}
